﻿using System;
using System.Collections.Generic;

using NUnit.Framework;

using Yawf.Core;
using Yawf.Test;


using Spring.Context;
using Spring.Context.Support;

using Yawf.Core.Logs;

namespace Yawf.Data {
	[TestFixture]
	public class NHibernatePersisterTest {
		private NHibernatePersister persister;

		[SetUp]
		public virtual void SetUp() {
			persister = Initializer.Instance.GetObject("nhibernatePersister") as NHibernatePersister;
		}

		[Test]
		public void TestFindOne() {
			DummyClass o;

			persister = ContextRegistry.GetContext().GetObject("nhibernatePersister") as NHibernatePersister;

			o = persister.FindOne<DummyClass>("SELECT d FROM Yawf.Test.DummyClass d WHERE d.Id = ?", new Object[] { 5 });
			Assert.IsNotNull(o,"object returned by FindOne is null");
			Assert.IsInstanceOfType(typeof(DummyClass),o,"Object returned by FindOne is ont a DummyClass");
		}

		[Test]
		public void TestFindList() {
			IList<DummyClass> hits;

			hits = persister.FindList<DummyClass>("SELECT d FROM Yawf.Test.DummyClass d", null);
			Assert.IsNotNull(hits,"hits returned by FindList is null");
			Assert.IsTrue(hits.Count > 0,"hits.Count returned by FindList <= 0");

			hits = persister.FindList<DummyClass>("SELECT d FROM Yawf.Test.DummyClass d");
			Assert.IsNotNull(hits,"hits returned by FindList is null");
			Assert.IsTrue(hits.Count > 0,"hits.Count returned by FindList <= 0");

			hits = persister.FindList<DummyClass>("SELECT d FROM Yawf.Test.DummyClass d", null, 1, 3);
			Assert.IsNotNull(hits,"hits returned by FindList is null");
			Assert.IsTrue(hits.Count == 3,"hits.Count returned by FindList != 3");
		}

		[Test]
		public void TestInsertUpdateDelete() {
			DummyClass dummyInserted,tmpDummy;

			dummyInserted = new DummyClass();
			dummyInserted.Value1 = 9999;
			dummyInserted.Value2 = 99;
			dummyInserted.Value3 = 99.99M;
			dummyInserted.Value4 = "Nueve Nueve";
			Assert.AreEqual(0,dummyInserted.Id,"Id != 0");
			persister.Insert("not used",dummyInserted);

			Assert.IsTrue(dummyInserted.Id > 0,"dummyInserted.Id <= 0 After insert");

			tmpDummy = persister.FindOne<DummyClass>("SELECT d FROM Yawf.Test.DummyClass d WHERE d.Id = ?", new Object[] { dummyInserted.Id });
			Assert.IsNotNull(tmpDummy,"Dummy class was not inserted");
			Assert.AreEqual(dummyInserted.Id,tmpDummy.Id,"Inserted id != to loaded Id");
			Assert.AreEqual(dummyInserted.Value1,9999,"Value1 was not saved with 9999");
			Assert.AreEqual(dummyInserted.Value2,99,"Value2 was not saved with 99");
			Assert.AreEqual(dummyInserted.Value3,99.99,"Value3 was not saved with 99.99");
			Assert.AreEqual(dummyInserted.Value4,"Nueve Nueve","Value4 was not saved with 'Nueve Nueve'");

			dummyInserted.Value1 = 19999;
			dummyInserted.Value2 = 199;
			dummyInserted.Value3 = 199.99M;
			dummyInserted.Value4 = "Uno Nueve Nueve"; 
			persister.Update("not used",dummyInserted);
			tmpDummy = persister.FindOne<DummyClass>("SELECT d FROM Yawf.Test.DummyClass d WHERE d.Id = ?", new Object[] { dummyInserted.Id });
			Assert.IsNotNull(tmpDummy,"Dummy class was not updated");
			Assert.AreEqual(dummyInserted.Id,tmpDummy.Id,"Inserted id != to loaded Id");
			Assert.AreEqual(dummyInserted.Value1,19999,"Value1 was not saved with 19999");
			Assert.AreEqual(dummyInserted.Value2,199,"Value2 was not saved with 199");
			Assert.AreEqual(dummyInserted.Value3,199.99,"Value3 was not saved with 199.99");
			Assert.AreEqual(dummyInserted.Value4,"Uno Nueve Nueve","Value4 was not saved with 'Uno Nueve Nueve'");

			persister.Delete("not used",dummyInserted);
			tmpDummy = persister.FindOne<DummyClass>("SELECT d FROM Yawf.Test.DummyClass d WHERE d.Id = ?", new Object[] { dummyInserted.Id });
			Assert.IsNull(tmpDummy,"Dummy class was not deleted");
		}						// TestInsertUpdateDelete
	}						// Class
}
